<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>web socket demo</title>
</head>
<body>
    <form >
        <textarea name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="on message" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="clear content" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
<script>
    var socket;
    if (window.WebSocket) {
        // socket = new WebSocket("ws://192.168.31.79:8081/testWebsocket");
        socket = new WebSocket("ws://localhost:8081/testWebsocket");
        socket.onmessage = function (e) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + e.data
        };

        socket.onopen = function (e) {
            var rt = document.getElementById("responseText");
            rt.value = "open........"
        };

        socket.onclose = function (e) {
            var rt = document.getElementById("responseText")
            rt.value = rt.value + "\n" + "连接关闭了.."
        };
    } else {
        alert("当前浏览器不支持websocketv")
    }

    function send(msg) {
        if (!window.socket) {
            return
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(msg);
        } else {
            alert("not open")
        }
    }
</script>
</html>